<?php

/*
 * 2021/7/15
 * 14:22
 * User: 2650332702@qq.com
 */

namespace App\Api\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Seckill extends Model
{
    use HasFactory;
    protected $table='cmc_store_seckill';

    static public function dataSorting($allow,$where,$time='')
    {
        return Seckill::orderBy($where,'desc')
            ->when($where=='start_time' ,function ($query) use ($time){
                $query->where('end_time','>',$time);
            })
            ->get($allow)
            ->toArray();
    }
    //获取秒杀正常的数量
    static public function getCount($time,$status = 0)
    {
        return Seckill::where('status',$status)->where('end_time','>',$time)->count();
    }
    //根据秒杀ID单个字段
    static public function skInGood($skid,$where)
    {
        return Seckill::where('id',$skid)->get([$where])->toArray()[0][$where];
    }

    public function getImagesAttribute($value)
    {
        return config('aliyun.cui').$this->attributes['images'];
    }

    //时间区间查询
    static public function timeInterval(string $day,array $allow_field)
    {
        return Seckill::orderBy('end_time','desc')
            ->where('start_time','like',"{$day}%")
            ->get($allow_field)
            ->toArray();
    }

    //减库存进行秒杀
    static public function inventoryReduction($id,$stock)
    {
        return Seckill::where('id',$id)->update(['stock'=>$stock]);
    }
    //获得订单详情数据
    static public function orderInfo($seckill_id)
    {
        return Seckill::where('id',$seckill_id)->get(['price','goods_id','seckill_name'])->toArray()[0];
    }

    static public function secInGoods($sec_id,$where)
    {
        $secGoodsId = Seckill::where('id',$sec_id)->get([$where])->toArray();

        $arr = [];

        $count = count($secGoodsId);

        for($i=0;$i<$count;$i++){

            $arr = Goods::where('goods_id',$secGoodsId[$i]['goods_id'])->select('goods_id','goods_name','goods_image','created_at')->get()->toArray();

        }

        return $arr;
    }
}
